<?php
class ThanhtichkhoacbsController extends AppController {

	var $name = 'Thanhtichkhoacbs';
	
    var $uses= array('Thanhtichkhoacb','Hockie','Tgiachidoancb','Chidoansv');
    var $components = array('RequestHandler'); 
    
	function index($phongtrao_id)
	{
		$this->Thanhtichkhoacb->recursive = 0;
		$this->set('thanhtichkhoacbs', $this->paginate(array('phongtraokhoa_id'=>$phongtrao_id)));
		$phongtrao = $this->Thanhtichkhoacb->Phongtraokhoa->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraokhoa.id'=>$phongtrao_id)));
		$this->set(compact('phongtrao','phongtrao_id'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Thanhtichkhoacb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('Thanhtichkhoacb', $this->Thanhtichkhoacb->read(null, $id));
	}

	function viewtt($mssv){
        if($mssv==null){
            $this->Session->setFlash(__('Mã số sinh viên không hợp lệ', true));
        }else{
            $hocky = $this->getHocKyHt();
    		$x = $hocky['Hocky']['ngaybatdau'];
    		$y = $hocky['Hocky']['ngayketthuc'];
            $thanhtichkhoacbs=$this->Thanhtichkhoacb->find('all',array('conditions'=>array('canbo_id'=>$mssv, array('Phongtraokhoa.ngayketthuc BETWEEN ? AND ?' => array($x,$y)))));
            //pr($thanhtichs);
            $this->set(compact('thanhtichkhoacbs'));
        }
    }

	function add($phongtrao_id=null)
	{
		if (!empty($this->data))
		{
			$this->Thanhtichkhoacb->create();
			
			$this->data['Thanhtichkhoacb']['phongtraokhoa_id'] = $phongtrao_id;
			if(!isset($this->data['Thanhtichkhoacb']['canbo_id']))
				$this->data['Thanhtichkhoacb']['canbo_id'] = $this->data['canbo_id'];
			if ($this->Thanhtichkhoacb->save($this->data))
			{
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'add',$phongtrao_id));
			}
			else
			{
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true),'error');
			}
		}
		
		$this->Thanhtichkhoacb->recursive = 0;
		$this->set('thanhtichkhoacbs', $this->paginate(array('phongtraokhoa_id'=>$phongtrao_id)));
		$phongtrao = $this->Thanhtichkhoacb->Phongtraokhoa->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraokhoa.id'=>$phongtrao_id)));
    	$khoa_id = $phongtrao['Phongtraokhoa']['khoa_id'];
    	
		$querystring= "select Tgiachidoancb.chidoansv_id
    					from tgiachidoancbs as Tgiachidoancb, (select a.chidoansv_id, count(canbo_id) as sl from tgiachidoancbs a where ngaychuyen is NULL group by a.chidoansv_id) as Tgiachidoancb1
    					where Tgiachidoancb.chidoansv_id = Tgiachidoancb1.chidoansv_id and Tgiachidoancb1.sl>0 and ngaychuyen is NULL
    						group by Tgiachidoancb.chidoansv_id";
    	$laycd_act = $this->Thanhtichkhoacb->query($querystring);
    	$cd_id = array();
    	foreach ($laycd_act as $cd) 
    	{
    		$cd_id[] = $cd['Tgiachidoancb']['chidoansv_id'];
    	}
    	
    	$this->Chidoansv->displayField='tenchidoan' ;
    	$chidoans = $this->Chidoansv->find('list',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id'=>null,'id'=>$cd_id),'fields'=>array('chidoan')));
    	$chidoan_id =array_keys($chidoans);
    	$cbdaco = $this->Thanhtichkhoacb->find('list',array('conditions'=>array('phongtraokhoa_id'=>$phongtrao_id),'fields'=>array('canbo_id')));
    	$cbs = $this->Tgiachidoancb->find('all',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id,'NOT'=>array('Canbo.id'=>$cbdaco)),'fields'=>array('Canbo.id','Canbo.mscb','Canbo.hoten')));
		foreach ($cbs as $cb)
    	{
    		$canbos[$cb['Canbo']['id']]=$cb['Canbo']['mscb'].'-'.$cb['Canbo']['hoten'];
    	}
		$this->set(compact('canbos', 'phongtrao','phongtrao_id','chidoans'));
	}

	function edit($id = null,$phongtrao_id) {
		if (!empty($this->data))
		{
			$this->Thanhtichkhoacb->create();
			
			$this->data['Thanhtichkhoacb']['phongtraokhoa_id'] = $phongtrao_id;
			if(!isset($this->data['Thanhtichkhoacb']['canbo_id']))
				$this->data['Thanhtichkhoacb']['canbo_id'] = $this->data['canbo_id'];
			if ($this->Thanhtichkhoacb->save($this->data))
			{
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$phongtrao_id));
			}
			else
			{
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true),'error');
			}
		}
		
		if (empty($this->data))
		{
			$this->data = $this->Thanhtichkhoacb->read(null, $id);
		}
		$canbo_id = $this->data['Thanhtichkhoacb']['canbo_id'];
		
		$phongtrao = $this->Thanhtichkhoacb->Phongtraokhoa->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraokhoa.id'=>$phongtrao_id)));
    	$khoa_id = $phongtrao['Phongtraokhoa']['khoa_id'];
    	$chidoans = $this->Chidoansv->find('list',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id'=>null,'id'=>$cd_id),'fields'=>array('chidoan')));
    	$chidoan_id =array_keys($chidoans);
    	$cbdaco = $this->Thanhtichkhoacb->find('list',array('conditions'=>array('phongtraokhoa_id'=>$phongtrao_id,'NOT'=>array('canbo_id'=>$canbo_id)),'fields'=>array('canbo_id')));
		$cbs = $this->Tgiachidoancb->find('all',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id,'NOT'=>array('Canbo.id'=>$cbdaco)),'fields'=>array('Canbo.id','Canbo.mscb','Canbo.hoten')));
		foreach ($cbs as $cb)
    	{
    		$canbos[$cb['Canbo']['id']]=$cb['Canbo']['mscb'].'-'.$cb['Canbo']['hoten'];
    	}
		$this->set(compact('canbos', 'phongtrao','phongtrao_id','chidoans'));
	}

	function delete($id = null,$phongtrao_id) {
		if (!$id) {
			$this->Session->setFlash(__('Giá trị không tồn tại', true));
			$this->redirect(array('action'=>'index',$phongtrao_id));
		}
		if ($this->Thanhtichkhoacb->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action'=>'index',$phongtrao_id));
		}
		$this->Session->setFlash(__('Không thể xóa vui lòng thử lại', true),'error');
		$this->redirect(array('action' => 'index',$phongtrao_id));
	}

	function laycanbo($phongtrao_id)
	{
		$this->layout = 'ajax';
		$chidoan_id = $this->data['Thanhtichkhoacb']['chidoan_id'];
		
		$cbdaco = $this->Thanhtichkhoacb->find('list',array('conditions'=>array('phongtraokhoa_id'=>$phongtrao_id),'fields'=>array('canbo_id')));
    	$cbs = $this->Tgiachidoancb->find('all',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id,'NOT'=>array('Canbo.id'=>$cbdaco)),'fields'=>array('Canbo.id','Canbo.mscb','Canbo.hoten')));
		foreach ($cbs as $cb)
    	{
    		$canbos[$cb['Canbo']['id']]=$cb['Canbo']['mscb'].'-'.$cb['Canbo']['hoten'];
    	}
		$this->set(compact('canbos'));
	}

/*	
	function index() {
		$this->Thanhtichkhoacb->recursive = 0;
		$this->set('thanhtichkhoacbs', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid thanhtichkhoacb', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('thanhtichkhoacb', $this->Thanhtichkhoacb->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Thanhtichkhoacb->create();
			if ($this->Thanhtichkhoacb->save($this->data)) {
				$this->Session->setFlash(__('The thanhtichkhoacb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The thanhtichkhoacb could not be saved. Please, try again.', true));
			}
		}
		$phongtraokhoas = $this->Thanhtichkhoacb->Phongtraokhoa->find('list');
		$canbos = $this->Thanhtichkhoacb->Canbo->find('list');
		$this->set(compact('phongtraokhoas', 'canbos'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid thanhtichkhoacb', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Thanhtichkhoacb->save($this->data)) {
				$this->Session->setFlash(__('The thanhtichkhoacb has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The thanhtichkhoacb could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Thanhtichkhoacb->read(null, $id);
		}
		$phongtraokhoas = $this->Thanhtichkhoacb->Phongtraokhoa->find('list');
		$canbos = $this->Thanhtichkhoacb->Canbo->find('list');
		$this->set(compact('phongtraokhoas', 'canbos'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for thanhtichkhoacb', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Thanhtichkhoacb->delete($id)) {
			$this->Session->setFlash(__('Thanhtichkhoacb deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Thanhtichkhoacb was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

*/
}
